<template>
  <div>
    <tableComponents
      :isHavePagination="false"
      :isShow="false"
      :isHaveExport="false"
      @toAdd="showAdd()"
      :isShowBottom="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list"
          v-loading="loading"
          header-row-class-name="head_color"
          stripe
          tooltip-effect="dark"
          style="width: 99.9%"
        >
          <el-table-column label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input
                @change="changeSort($event, scope.row)"
                v-model.number="scope.row.sort"
                size="small"
                style="width: 80px"
              ></el-input>
            </template>
          </el-table-column>

          <el-table-column label="充值金额" min-width="130">
            <template slot-scope="scope">
              {{ scope.row.price | keepTwoNum }}
            </template>
          </el-table-column>

          <el-table-column label="赠送类型" min-width="130">
            <template slot-scope="scope">
              {{
                scope.row.types == "integral"
                  ? "积分"
                  : scope.row.types == "money"
                  ? "余额"
                  : scope.row.types == "coupon"
                  ? "优惠券"
                  : "无"
              }}
            </template>
          </el-table-column>

          <el-table-column label="赠送信息" min-width="130">
            <template slot-scope="scope">
              <div
                v-if="
                  scope.row.types == 'integral' || scope.row.types == 'money'
                "
              >
                {{ scope.row.send | keepTwoNum }}
              </div>
              <div v-if="scope.row.types == 'coupon'">
                <p v-for="(item, i) in toArr(scope.row.coupon)" :key="i">
                  {{ item.title }}：{{ item.num }} 张
                </p>
              </div>
              <div v-else>无</div>
            </template>
          </el-table-column>

          <el-table-column label="显示" min-width="65">
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.show"
                active-color="#1aad19"
                inactive-color="#ffffff"
                active-text="on"
                inactive-text="off"
                active-value="1"
                @change="changeSort($event, scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="100">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="showAdd(scope.row)"
                type="primary"
                >编辑</el-button
              >
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="del(scope.row.id)"
                type="danger"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :title="printObj.id ? '编辑' : '添加'"
      :append-to-body="true"
      @close="show = false"
      :visible.sync="show"
      width="800px"
    >
      <el-form :model="printObj" label-width="120px">
        <el-form-item label="充值金额">
          <el-col :span="16">
            <el-input v-model="printObj.price" size="small"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="赠送类型">
          <el-col :span="24">
            <el-radio-group v-model="printObj.types">
              <el-radio label="integral">积分</el-radio>
              <el-radio label="money">余额</el-radio>
              <el-radio label="coupon">优惠券</el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>

        <el-form-item label="优惠券" v-if="printObj.types == 'coupon'">
          <el-col :span="6">
            <el-button
              :loading="$store.state.isLoading"
              size="mini"
              type="primary"
              @click="xzshop('xzShop')"
              >选择优惠券</el-button
            >
          </el-col>
          <el-col :span="24" v-for="(item, i) in printObj.coupon" :key="i">
            {{ item.title }}&nbsp;&nbsp;&nbsp;数量：
            <el-input
              v-model="item.num"
              size="small"
              style="width: 120px"
            ></el-input
            >&nbsp;张&nbsp;&nbsp;&nbsp;&nbsp;
            <el-button
              :loading="$store.state.isLoading"
              size="mini"
              type="danger"
              @click="del_packet(i)"
            >
              删除
            </el-button>
          </el-col>
        </el-form-item>
        <el-form-item
          label="金额"
          v-if="printObj.types == 'integral' || printObj.types == 'money'"
        >
          <el-col :span="16">
            <el-input v-model="printObj.send" size="small"></el-input>
          </el-col>
        </el-form-item>

        <div class="tc">
          <el-button
            :loading="$store.state.isLoading"
            type="primary"
            @click="save"
            >保存</el-button
          >
        </div>
      </el-form>
    </el-dialog>
    <keep-alive>
      <component
        :is="current"
        :show="shows"
        :isAll="true"
        showType="list"
        @sendShop="getSend"
        @isshow="getShow"
      ></component>
    </keep-alive>
  </div>
</template>
<script>
import xzShop from "@/components/currency/xz_packet.vue";
export default {
  data() {
    return {
      loading: false,
      list: [],
      show: false,
      printObj: {},
      shows: true,
      current: "",
    };
  },
  components: { xzShop },
  methods: {
    toArr(str) {
      if (str) {
        return JSON.parse(str);
      }
      return [];
    },
    xzshop(str) {
      this.current = str;
      this.shows = true;
    },
    getShow(val) {
      this.shows = val;
    },
    getSend(row) {
      this.printObj.coupon.push({
        id: row.id,
        title: row.title,
        num: 1,
      });
    },
    del_packet(i) {
      this.printObj.coupon.splice(i, 1);
    },
    changeSort(val, item) {
      this.$api.getUser.recharge_money_saveedit(item).then((res) => {
        this.get_data();
      });
    },
    showAdd(item) {
      if (item) {
        this.printObj = JSON.parse(JSON.stringify(item));
        this.printObj.price = this.$fnc.toFixedZ(this.printObj.price);
        this.printObj.send = this.$fnc.toFixedZ(this.printObj.send);
        var coupon = [];
        if (this.printObj.coupon) {
          try {
            coupon = JSON.parse(this.printObj.coupon);
          } catch {
            coupon = [];
          }
        }
        this.printObj.coupon = coupon;
      } else {
        this.printObj = {
          price: "",
          send: "money",
          coupon: "",
        };
      }
      this.show = true;
    },
    del(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getUser.recharge_money_del({ id: id }).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.get_data();
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    get_data() {
      this.loading = true;
      this.$api.getUser.recharge_money_lists({}).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    save() {
      var params = JSON.parse(JSON.stringify(this.printObj));
      params.coupon = JSON.stringify(params.coupon);
      if (params.id) {
        this.$api.getUser.recharge_money_saveedit(params).then((res) => {
          if (res.data.code == 200) {
            this.$swal("操作成功！", "", "success");
            this.show = false;
            this.get_data();
          } else {
            this.$swal("操作失败！", res.data.result, "error");
          }
        });
      } else {
        this.$api.getUser.recharge_money_saveadd(params).then((res) => {
          if (res.data.code == 200) {
            this.$swal("操作成功！", "", "success");
            this.show = false;
            this.get_data();
          } else {
            this.$swal("操作失败！", res.data.result, "error");
          }
        });
      }
    },
  },
  created() {
    this.get_data();
  },
  computed: {},
};
</script>

<style lang="less" scoped>
</style>
